package org.vaadin.vaadinjit.widgetset.client.ui;

import com.google.gwt.json.client.JSONObject;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.ValueMap;


/**
 * Client-side base class for AreaChart, BarChart and PieChart.
 * 
 * @author jonkka
 *
 * @param <T> the used overlay type
 */
public abstract class VJitChartWidget<T extends JitChartOverlay> 
											extends VJitWidget<T> {

	public static String HOVER_LABEL_IDENTIFIER = "hoverLabel";
	
	protected int leftMargin = 0;
	protected int topMargin = 0; 
	protected int rightMargin = 0;
	protected int bottomMargin = 0;

	private String hoveredLabel = "";
	
	private boolean sendHoverLabelEvents;
	
	/** Tells if data has been loaded  */
	private boolean dataLoaded = false;

	
	@Override
	protected void initialize(UIDL initUidl) {
		super.initialize(initUidl);
		
		if (initUidl.hasAttribute("margins")) {
			ValueMap marginMap = initUidl.getMapAttribute("margins");
			this.leftMargin = marginMap.getInt("leftMargin");
			this.rightMargin = marginMap.getInt("rightMargin");
			this.topMargin = marginMap.getInt("topMargin");
			this.bottomMargin = marginMap.getInt("bottomMargin");
		}
	}
	

	public void onHoverLabel(String label) {
		if (label.equals(hoveredLabel))
			return;
		
		hoveredLabel = label;
		
		// Only send events if the server has told us so...
		if (sendHoverLabelEvents) {
			client.updateVariable(
					paintableId, HOVER_LABEL_IDENTIFIER, hoveredLabel, true);
		}
	}

	@Override
	public void setHeight(String height) {
		super.setHeight(height);
		
		if (isInitialized()) {
			handleNativeResize(getVisualization());
		}
	}
	
	
	public void setWidth(String width) {
		super.setWidth(width);
		
		if (isInitialized()) {
			handleNativeResize(getVisualization());
		}
	}
	
	
	@Override
	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		super.updateFromUIDL(uidl, client);
		
		if (uidl.hasAttribute("receiveHoverLabelEvents")) {
			sendHoverLabelEvents = 
				uidl.getBooleanAttribute("receiveHoverLabelEvents");
		}
	}
	
	
	@Override
	protected void setupConfig(JitConfiguration config) {
		super.setupConfig(config);
		
		JitConfiguration margins = new JitConfiguration();
		margins.setOption("top", topMargin);
		margins.setOption("left", leftMargin);
		margins.setOption("right", rightMargin);
		margins.setOption("bottom", bottomMargin);
		
		config.setOption("Margin", margins);
		
		config.setOption("type", getUseGradients() ? "stacked:gradient" : "stacked");

		
	}
	
	@Override
	protected void loadJitData(JSONObject jsonData) {
		if (!dataLoaded || getNewDataLoaded()) {
			getVisualization().loadJSON(jsonData.getJavaScriptObject());
			dataLoaded = true;
			setNewDataLoaded(false);
		}
		// If data has already been loaded once: use updateJSON instead
		else {
			getVisualization().updateJSON(jsonData.getJavaScriptObject());
		}
	}
	

	protected abstract void handleNativeResize(T overlay);
}
